﻿
#include "MainWnd.h"
#include "../util/StringUtil.h"
#include "WuDong.h"
#include "SelectWnd.h"
#define CloseButtonControlName _T("BtnClose")
#define MinButtonControlName _T("BtnMin")
#define MaxButtonControlName _T("BtnMax")
#define RestoreButtonControlName _T("BtnRestore")
//////////////////////////////////////////////////////////////////////////
///
DUI_BEGIN_MESSAGE_MAP(CMainWnd, WindowImplBase)
DUI_END_MESSAGE_MAP()

CMainWnd::CMainWnd()
{
	WuDong wd;
	long lRet = 0;
	long x, y = 0;
	wd.BindWindow(0, L"dx2", L"normal", L"normal", 0, &lRet);
	// wd.Capture(10, 10, 100, 100, L"test.png", &lRet);
	// wd.CmpColor(0, 1000, L"#FF0000", 1, &lRet);
	// wd.FindColor(0, 1000, 10, 2000, L"#FF0000", 1, 0, &x, &y, &lRet);
	wd.FindPic(0, 0, 1000, 1000, L"search.png", 0.8, &x, &y, &lRet);
}

CMainWnd::~CMainWnd()
{
}

CControlUI *CMainWnd::CreateControl(LPCTSTR pstrClass)
{
	// if (lstrcmpi(pstrClass, _T("CVImage" )) == 0)
	// {
	// 	return new CVImageUI();
	// }
	// else if (lstrcmpi(pstrClass, _T("PreviewImage" )) == 0)
	// {
	// 	return new PreviewImageUI();
	// }

	return NULL;
}

void CMainWnd::InitWindow()
{
	m_pm.SetDPI(CDPI::GetMainMonitorDPI());
	m_pLabelStatusWindow = static_cast<CLabelUI *>(m_pm.FindControl(_T("label_status")));
}

DuiLib::CDuiString CMainWnd::GetSkinFile()
{
	return _T("main_wnd.xml");
}

LPCTSTR CMainWnd::GetWindowClassName() const
{
	return _T("MainWnd");
}

UINT CMainWnd::GetClassStyle() const
{
	return CS_DBLCLKS;
}

void CMainWnd::OnFinalMessage(HWND hWnd)
{
	__super::OnFinalMessage(hWnd);
}

void CMainWnd::Notify(TNotifyUI &msg)
{
	CDuiString name = msg.pSender->GetName();
	if (msg.sType == _T("windowinit"))
	{
	}
	else if (_tcsicmp(msg.sType, _T("click")) == 0)
	{
		OnClick(msg.pSender);
	}
	else if (msg.sType == _T("textchanged"))
	{
		CEditUI *pEdit = (CEditUI *)msg.pSender;
	}
	else if (msg.sType == _T("selectchanged"))
	{
	}
	else if (msg.sType == _T("valuechanged"))
	{
	}
	return WindowImplBase::Notify(msg);
}
void CMainWnd::OnClick(CControlUI *pSender)
{
	if (_tcsicmp(pSender->GetName(), MinButtonControlName) == 0)
	{
		SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);

		return;
	}
	else if (_tcsicmp(pSender->GetName(), MaxButtonControlName) == 0)
	{
		SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
		return;
	}
	else if (_tcsicmp(pSender->GetName(), RestoreButtonControlName) == 0)
	{
		SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
		return;
	}
	else if (_tcsicmp(pSender->GetName(), CloseButtonControlName) == 0)
	{
		Close();
		// PostQuitMessage(0);
		//::ShowWindow(m_hWnd, SW_HIDE);
	}
	else if (_tcsicmp(pSender->GetName(), _T("btn_select_window")) == 0)
	{
		SelectWnd::StartCapture([this](HWND hwnd)
								{ 
									CDuiString statusStr;
									TCHAR titleBuf[256] = {0};
									GetWindowTextW(hwnd, titleBuf, 256);
									statusStr.Format(L"窗口句柄: 0x%08X, 标题: %s", hwnd, titleBuf);
									m_pLabelStatusWindow->SetText(statusStr.GetData()); });
	}
}

LRESULT CMainWnd::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL &bHandled)
{
	bHandled = FALSE;

	PostQuitMessage(0);
	return 0;
}

LRESULT CMainWnd::HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
{
	if (uMsg == WM_DESTROY)
	{
		::PostQuitMessage(0L);
		bHandled = TRUE;
		return 0;
	}

	bHandled = FALSE;
	return 0;
}
LRESULT CMainWnd::OnLButtonDown(UINT, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
{

	bHandled = FALSE;
	return 0;
}
LRESULT CMainWnd::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
{
#if defined(WIN32) && !defined(UNDER_CE)
	BOOL bZoomed = ::IsZoomed(m_hWnd);
	LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	if (::IsZoomed(m_hWnd) != bZoomed)
	{
		if (!bZoomed)
		{
			CControlUI *pControl = static_cast<CControlUI *>(m_pm.FindControl(MaxButtonControlName));
			if (pControl)
				pControl->SetVisible(false);
			pControl = static_cast<CControlUI *>(m_pm.FindControl(RestoreButtonControlName));
			if (pControl)
				pControl->SetVisible(true);
		}
		else
		{
			CControlUI *pControl = static_cast<CControlUI *>(m_pm.FindControl(MaxButtonControlName));
			if (pControl)
				pControl->SetVisible(true);
			pControl = static_cast<CControlUI *>(m_pm.FindControl(RestoreButtonControlName));
			if (pControl)
				pControl->SetVisible(false);
		}
	}

#else
	return __super::OnSysCommand(uMsg, wParam, lParam, bHandled);
#endif
	return 0;
}
